from django.shortcuts import render
from django.http import request,HttpResponse,Http404,JsonResponse
import time

#导入分词文件
from chatbot.cut_words import cut_match
from chatbot.models import MsgInfo





# Create your views here.

#智能客服-主界面
def robot(request):

    return render(request,'chatbot/robot.html')

#智能客服-功能-自动回复
def robot_func(request):

    # data是获得的前端顾客输入的内容 type==str
    sentence=request.POST.get('msg')

    #进行分词匹配
    cm=cut_match.CutMatch(sentence)
    cm.match()

    msg_reply=cm.ans
    #print(cm.ans)

    # 这是后端要传递到前端的内容
    return JsonResponse({'msg':msg_reply})

    

# 人工客服(顾客端)-主界面
def manbot(request):

    return render(request,'chatbot/manbot.html')

# 人工客服(顾客端)-按钮‘发送’对应的功能
def manbot_cus_send(request):

    # data是获得的前端顾客输入的内容 type==str
    sentence=request.POST.get('msg')

    mi=MsgInfo()
    mi.name='顾客'
    mi.message=sentence
    
    mi.save()
    print(sentence)

    # 这是后端要传递到前端的内容
    return JsonResponse({'msg':sentence})

# 人工客服(顾客端)-接收消息
def manbot_cus_get(request):
    sentence=request.POST.get('msg')

    #接收回复的消息
    mi=MsgInfo.objects
    mi_last=mi.last()

    if mi_last.name=='服务':
        print('接收到了消息')
        return JsonResponse({'msg':mi_last.message})
    else:
        print('没有接收到消息')

        return JsonResponse({'msg':0})

# 人工客服(服务端)-主界面
def manbot_rep(request):

    return render(request,'chatbot/manbot_rep.html')

# 人工客服(服务端)-按钮‘发送’对应的功能
def manbot_rep_send(request):

    # data是获得的前端顾客输入的内容 type==str
    sentence=request.POST.get('msg')

    mi=MsgInfo()
    mi.name='服务'
    mi.message=sentence

    #print(sentence)
    
    mi.save()

    # 这是后端要传递到前端的内容
    return JsonResponse({'msg':sentence})

# 人工客服(服务端)-接收消息
def manbot_rep_get(request):
    sentence=request.POST.get('msg')

    #接收回复的消息
    mi=MsgInfo.objects
    mi_last=mi.last()

    if mi_last.name=='顾客':
        print('接收到了消息')
        return JsonResponse({'msg':mi_last.message})
    else:
        print('没有接收到消息')

        return JsonResponse({'msg':0})










def botmain(request):
    '''
    客服主界面+'购买产品'问题界面
    '''
    return render(request,'chatbot/botmain.html')

def buying(request):
    '''
    '购买产品'问题界面
    '''
    return render(request,'chatbot/buying.html')

def repairing(request):
    '''
    '维修保养'问题界面
    '''
    return render(request,'chatbot/repairing.html')

def generating(request):
    '''
    '充电规划'问题界面
    '''
    return render(request,'chatbot/generating.html')

def contact(request):
    '''
    '联系我们'问题界面
    '''
    return render(request,'chatbot/contact.html')



# 充电规划的问题4个
def generating_q1(request):

    return render(request,'chatbot/generating/g_q1.html')

def generating_q2(request):
    
    return render(request,'chatbot/generating/g_q2.html')

def generating_q3(request):
    
    return render(request,'chatbot/generating/g_q3.html')

def generating_q4(request):
    
    return render(request,'chatbot/generating/g_q4.html')

#维修保养问题
def repairing_q1(request):

    return render(request,'chatbot/repairing/r_q1.html')
def repairing_q2(request):

    return render(request,'chatbot/repairing/r_q2.html')
def repairing_q3(request):

    return render(request,'chatbot/repairing/r_q3.html')
def repairing_q4(request):

    return render(request,'chatbot/repairing/r_q4.html')

#智能用车问题
def buying_q1(request):

    return render(request,'chatbot/buying/b_q1.html')
def buying_q2(request):

    return render(request,'chatbot/buying/b_q2.html')
def buying_q3(request):

    return render(request,'chatbot/buying/b_q3.html')
def buying_q4(request):

    return render(request,'chatbot/buying/b_q4.html')